import os

from pyltp import Segmentor

# -*- coding: utf-8 -*-
import os
import jieba

LTP_DIR = "D:\\workspace\\source\\python\\ltp_data_v3.4.0"
LTP_DATA_DIR = LTP_DIR  # ltp模型目录的路径
cws_model_path = os.path.join(LTP_DATA_DIR, 'cws.model')  # 分词模型路径，模型名称为`cws.model`
pos_model_path = os.path.join(LTP_DATA_DIR, 'pos.model')  # 词性标注模型路径，模型名称为`pos.model`
ner_model_path = os.path.join(LTP_DATA_DIR, 'ner.model')  # 命名实体识别模型路径，模型名称为`ner.model`
par_model_path = os.path.join(LTP_DATA_DIR, 'parser.model')  # 依存句法分析模型路径，模型名称为`parser.model`
srl_model_path = os.path.join(LTP_DATA_DIR, 'srl')  # 语义角色标注模型目录路径，模型目录为`srl`。注意该模型路径是一个目录，而不是一个文件。

from pyltp import SentenceSplitter
from pyltp import Segmentor
from pyltp import Postagger
from pyltp import NamedEntityRecognizer
from pyltp import Parser
from pyltp import SementicRoleLabeller


# 创建停用词表
def stopwordslist(filepath):
    stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
    return stopwords


# 分句，也就是将一片文本分割为独立的句子
def sentence_splitter(sentence):
    sents = SentenceSplitter.split(sentence)  # 分句
    print('\n'.join(sents))


# 分词
def segmentor(sentence):
    segmentor = Segmentor()  # 初始化实例
    segmentor.load(cws_model_path)  # 加载模型
    # segmentor.load_with_lexicon('cws_model_path', 'D:\pyprojects\LTP\ltp_data\dict.txt') #加载模型   使用用户自定义字典的高级分词
    words = segmentor.segment(sentence)  # 分词
    # 默认可以这样输出
    # print('/'.join(words))
    # 可以转换成List 输出
    words_list = list(words)
    segmentor.release()  # 释放模型
    return words_list


# 词性标注
def posttagger(words):
    postagger = Postagger()  # 初始化实例
    postagger.load(pos_model_path)  # 加载模型
    postags = postagger.postag(words)  # 词性标注
    # for word, tag in zip(words, postags):
    #   print(word + '/' + tag)
    postagger.release()  # 释放模型
    return postags


# 命名实体识别
def ner(words, postags):
    recognizer = NamedEntityRecognizer()  # 初始化实例
    recognizer.load(ner_model_path)  # 加载模型
    netags = recognizer.recognize(words, postags)  # 命名实体识别
    # for word, ntag in zip(words, netags):
    #   print(word + '/' + ntag)
    recognizer.release()  # 释放模型
    return netags


testword="反复推明五运七气之秘以立补泄之法"
words = segmentor(testword)
print(words)

# stopwords = stopwordslist('D:/2181729/stop_words.txt')
# final = ''
# f1 = open('D:/2181729/nerfcdata/30.txt', 'w', encoding='UTF-8')
# with open('D:/2181729/data/30.txt', 'r', encoding='UTF-8') as f:
#     for line in f:
#         segs = jieba.cut(line, cut_all=False)
#         for seg in segs:
#             if seg not in stopwords:
#                 final += seg
#
# words = segmentor(final)
# postags = posttagger(words)
# netags = ner(words, postags)
#
# tags = []
# dict = []
#
# for word, ntag in zip(words, netags):
#     if (ntag != 'O'):  # 过滤非命名实体
#         tags.append(ntag)
#         if (ntag not in dict):
#             dict.append(ntag)
#         # print(word + '/' + ntag)
#         f1.write(word + ':' + ntag + '\r\n')
#
# for tag in dict:
#     num = tags.count(tag)
#     print(tag + ":" + str(num))
#     f1.write(tag + ":" + str(num) + '\r\n')
# f1.close()
